﻿using AutoMapper;
using SIG.Infrastructure.Configs;
using SIG.Model.Identity;
using SIG.Resources.Admin;
using SIG.Service;
using SIG.Service.Identity;
using SIG.ViewModel;
using SIG.ViewModel.Admin.Menus;
using SIG.ViewModel.Admin.Roles;
using SIG.WebMVC.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;


namespace SIG.WebMVC.Areas.Admin.Controllers
{
    [SIGAuth]
    public class RoleController : BaseController
    {
        IRoleService _RoleService;
        IMenuService _menuService;
        IUserService _userService;
        private readonly IMapper _mapper;
        public RoleController(IRoleService RoleService, IMenuService menuService, IUserService userService, IMapper mapper)
        {
            _RoleService = RoleService;
            _menuService = menuService;
            _userService = userService;
            _mapper = mapper;
        }

        // GET: Role
        public ActionResult Index()
        {
            var roles = _RoleService.FindBy(r => r.Id != SettingsManager.Role.Founder);

            return View(roles);
        }

        [AllowAnonymous]
        public PartialViewResult List()
        {
            var roles = _RoleService.FindBy(r=>r.Id!=SettingsManager.Role.Founder);
            return PartialView("_List",roles);
        }

        

        [HttpGet]
        // GET: Roles/Create
        public ActionResult SetRoleMenus(int id)
        {
            Role vRole = _RoleService.GetById(id);
            var menus = _menuService.GetMenusByCategoryId(SettingsManager.Menu.BackMenuCId);

            SetRoleMenusVM vm = new SetRoleMenusVM
            {
                RoleId = id,
                Menus = _mapper.Map<List<Menu>, List<MenuVM>>(menus),
                MenuIds = vRole.Menus.Select(m => m.Id).ToArray()
            };
              
            return PartialView("_SetRoleMenus", vm);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SetRoleMenus(int RoleId, int[] menuId)
        {
          
            if (RoleId > 0)
            {              
                _RoleService.SetRoleMenus(RoleId, menuId); 
                return Json(AR, JsonRequestBehavior.DenyGet);                 
            }
            AR.Setfailure("编辑角色权限失败");
            return Json(AR, JsonRequestBehavior.DenyGet); 
        }

        [HttpGet]   
        public ActionResult EditRole(int? id)
        {
            Role role = (id>0)? _RoleService.GetById(id): new Role();
            return PartialView("_EditRole", role);
        }

  
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditRole([Bind(Include = "Id,RoleName,Description")] Role role)
        {
           

            if (ModelState.IsValid)
            {
                if (role.Id > 0)
                {
                    Role vRole = _RoleService.GetById(role.Id);
                    if (!vRole.IsSys)
                    {
                        vRole.RoleName = role.RoleName;
                        vRole.Description = role.Description;
                        _RoleService.Update(vRole);
                    }
                    else
                    {
                        //  return new HttpStatusCodeResult(500, "系统角色不可编辑");
                        AR.SetWarning("系统角色不可编辑");
                        return Json(AR, JsonRequestBehavior.DenyGet);
                    }
                    
                }
                else
                {
                    _RoleService.Create(role);
                }

                var roles = _RoleService.FindBy(r => r.Id != SettingsManager.Role.Founder);
                AR.Data = RenderPartialViewToString("_List", roles);
                AR.SetSuccess(String.Format(Messages.AlertCreateSuccess, EntityNames.Menu));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            else
            {
                //return new HttpStatusCodeResult(500, "编辑角色失败");

                AR.Setfailure("编辑角色失败");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            
        }



        // POST: Roles/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {

            Role role = _RoleService.GetById(id);
            if(role.IsSys)
            {        
                AR.SetWarning("系统角色，不可以删除！");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            if (role == null)
            {                
                AR.Setfailure("未找到此角色！");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            _RoleService.Delete(role);
            return Json(AR, JsonRequestBehavior.DenyGet);
           
        }


    }
}